﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Zhaoxi.CustomerControl.Controls
{
    /// <summary>
    /// Temperature.xaml 的交互逻辑
    /// </summary>
    public partial class Temperature : UserControl
    {
        //private double _value;

        //public double Value
        //{
        //    get { return _value; }
        //    set
        //    {
        //        _value = value;
        //        this.Refresh();
        //    }
        //}


        public double Value
        {
            get { return (double)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }

        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(double), typeof(Temperature),
                new PropertyMetadata(0.0, new PropertyChangedCallback(OnValueChanged)));

        private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            // 监听属性的变化过程
            (d as Temperature).Refresh();
        }



        // 温度的数据类型
        public TemperatureType TemperatureType { get; set; } = TemperatureType.Celsius;

        public Temperature()
        {
            InitializeComponent();

            // 当前控件尺寸发生变化时候触发
            this.SizeChanged += Temperature_SizeChanged;
        }

        private void Temperature_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            Refresh();
        }

        private void Refresh()
        {
            double height = this.RenderSize.Height;
            double width = height / 5;

            double temp = width / 6;// 单位宽度


            double outer_down_y = height -
                (Math.Sqrt(Math.Pow(3 * temp, 2) - Math.Pow(2 * temp, 2)) +
                3 * temp);
            string outer_path_data_str = $"M{temp} {2 * temp}" +
                $"A{2 * temp} {2 * temp} 0 0 1 {3 * temp} 0" +
                $"A{2 * temp} {2 * temp} 0 0 1 {5 * temp} {2 * temp}" +
                $"L{5 * temp} {outer_down_y}" +
                $"A{3 * temp} {3 * temp} 0 0 1 {3 * temp} {height}" +
                $"A{3 * temp} {3 * temp} 0 0 1 {temp} {outer_down_y}" +
                "z";

            // 需要把Data的字符串设置给Path的Data属性
            // 1、定义一个属性，绑定给Path的Data
            // 2、通过转换器  类型转换器
            var convter = TypeDescriptor.GetConverter(typeof(Geometry));
            this.outer_path.Data = (Geometry)convter.ConvertFrom(outer_path_data_str);

            double inner_down_y = height - (Math.Sqrt(Math.Pow(2 * temp, 2) - Math.Pow(temp, 2)) + 3 * temp);
            string inner_path_data_str =
                $"M{2 * temp} {2 * temp}" +
                $"A{temp} {temp} 0 0 1 {3 * temp} {temp}" +
                $"A{temp} {temp} 0 0 1 {4 * temp} {2 * temp}" +
                $"L{4 * temp} {inner_down_y}" +
                $"A{2 * temp} {2 * temp} 0 1 1 {2 * temp} {inner_down_y}" +
                "z";
            this.inner_path.Data = (Geometry)convter.ConvertFrom(inner_path_data_str);


            #region 刻度线
            this.scale_canvas.Children.Clear();
            height -= 8 * temp;
            double start_y = 1.5 * temp;
            double end_y = start_y;// 结束Y点陆续往下移

            double per_scale = height / 40;// 刻度步长
            int max = 60;// 最大最小值   这块暂时写死
            for (int i = 0; i < 41; i++)
            {
                end_y = start_y + per_scale * i;

                Line line = new Line();
                line.X1 = 5 * temp + 5;
                line.Y1 = start_y + per_scale * i;
                line.X2 = 5 * temp + (i % 2 == 0 ? 20 : 15);
                line.Y2 = start_y + per_scale * i;
                line.StrokeThickness = 1;
                line.Stroke = new SolidColorBrush(Color.FromArgb(255, 150, 150, 150));

                this.scale_canvas.Children.Add(line);

                if (i % 4 == 0)
                {
                    TextBlock tb = new TextBlock();
                    tb.Foreground = new SolidColorBrush(Color.FromArgb(255, 150, 150, 150));
                    tb.Height = 12;
                    tb.Text = (max - (i / 4) * 10).ToString();
                    tb.SetValue(Canvas.LeftProperty, 5 * temp + 25);
                    tb.SetValue(Canvas.TopProperty, start_y + per_scale * i - 8);

                    this.scale_canvas.Children.Add(tb);
                }
            }


            // 华氏度
            max = 140;
            for (int i = 0; i < 41; i++)
            {
                Line line = new Line();
                line.X1 = temp - 5;
                line.Y1 = start_y + per_scale * i;
                line.X2 = (i % 2 == 0 ? -3 : 2);
                line.Y2 = start_y + per_scale * i;
                line.StrokeThickness = 1;
                line.Stroke = new SolidColorBrush(Color.FromArgb(255, 150, 150, 150));

                this.scale_canvas.Children.Add(line);


                if (i % 4 == 0)
                {
                    TextBlock tb = new TextBlock();
                    tb.Foreground = new SolidColorBrush(Color.FromArgb(255, 150, 150, 150));
                    tb.TextAlignment = TextAlignment.Right;
                    tb.Height = 12;
                    tb.Text = (max - (i / 4) * 18).ToString();
                    tb.SetValue(Canvas.RightProperty, 6.5 * temp);
                    tb.SetValue(Canvas.TopProperty, start_y + per_scale * i - 8);

                    this.scale_canvas.Children.Add(tb);
                }
            }
            #endregion

            double value = -10;// 摄氏度
            #region 温度变化 
            double top = 0;
            if (TemperatureType == TemperatureType.Celsius)
            {
                top = start_y + per_scale * 24;// 零点
                top -= this.Value * (end_y - start_y) / 100;
            }
            else if (TemperatureType == TemperatureType.Fahrenheit)
            {
                top = start_y + per_scale * 31;
                top -= this.Value * (end_y - start_y) / 180;
            }
            this.bor.Margin = new Thickness(0, top, 0, 0);
            #endregion
        }
    }

    public enum TemperatureType
    {
        Celsius, Fahrenheit
    }
}
